<?php
	// Author: Andy Marti
	// Datum: 17.03.2014
	// ----------------------------------------------------
	// Beschreibung Datei:
	//   Empfängt die Daten der POST Aktionen und verarbeitet diese.
	//   Eine Webseite mit einer Erfolgsmeldung wird angezeigt und 
	//   entsprechend an die Ursprungswebseite weiter geleitet.
	//
	// ----------------------------------------------------
	// Änderungen:
	//
	// ----------------------------------------------------
	
	require_once 'loader.php';
	
	
	if(strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') {
		$ExterUserID = htmlspecialchars($_POST['ExternalUserID']);
		If ($_POST['Action'] == 'setUser') {
			$Member->getUserData($ExterUserID, $LoggedUserID);
			set_POSTtoUser($Member);
			$Member->setUserData();
			$reload = 'get_user.php';
			$action = 'Benutzer speichern.';
		}
		If ($_POST['Action'] == 'setMemberData') {
			$Member->getUserData($ExterUserID, $LoggedUserID);
			set_POSTtoMemberData($Member);
			$Member->setMemberData();
			$reload = 'get_user.php';
			$action = 'Mitgliederdaten speichern.';
		}
		If ($_POST['Action'] == 'newMember') {
			set_POSTtoUser($Member);
			set_POSTtoMemberData($Member);
			$Member->newUser($ExterUserID);
			$Member->newMember($ExterUserID);
			$reload = 'get_user.php';
			$action = 'Neues Mitglied speichern.';
		}
	
	}



	// ------------------------------------------------------
	// Header laden.
	// ------------------------------------------------------
	$index = $Header->createMetaItem('http-equiv', 'refresh');
	$Header->addMetaItem($index, 'content', '0;url='.$Constants->getConst('LOC_BASE_URL').$reload);
	$Header->printHeader();
?>
	
<table>
  <tr>
    <th><?php print($action); ?></th>
  </tr>
  <tr>
    <td>Die Daten werden verarbeitet. Sie werden gleich zurück weitergeleitet...</td>
  </tr>
   <tr>
    <td>Sollten Sie nach 5 Sek. noch nicht weiter geleitet worden, so klicken Sie <a href="<?php print($Constants->getConst('LOC_BASE_URL').$reload); ?>">hier...</a></td>
  </tr>
  </table>
	
	
<?php
	// ------------------------------------------------------
	// Footer laden.
	// ------------------------------------------------------
	include 'footer.php';
	
	
	/**
	 * Fill the data from POST to User
	 */
	function set_POSTtoUser($member) {
		if(strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') {
			// Set fields of User.
			If (isset($_POST['Title'])) $member->Title = htmlspecialchars($_POST['Title']);
			$member->FirstName = htmlspecialchars($_POST['Vorname']);
			$member->LastName = htmlspecialchars($_POST['Name']);
			$member->Birthday = htmlspecialchars($_POST['Geb']);
			$member->ExternalUserID = htmlspecialchars($_POST['ExternalUserID']);
			// Set fields of Address.
			$member->Address->Address = htmlspecialchars($_POST['Adresse']);
			$member->Address->PLZ = htmlspecialchars($_POST['PLZ']);
			$member->Address->City = htmlspecialchars($_POST['Ort']);
			// Set fields of Contact.
			$member->Contact->TelefonPrivate = htmlspecialchars($_POST['Tel-P']);
			$member->Contact->TelefonBusiness = htmlspecialchars($_POST['Tel-G']);
			$member->Contact->TelefonMobile = htmlspecialchars($_POST['Mobile']);
			$member->Contact->EmailPrivate = htmlspecialchars($_POST['E-Mail-P']);
			$member->Contact->EmailBusiness = htmlspecialchars($_POST['E-Mail-G']);
			$member->Contact->WebsitePrivate = htmlspecialchars($_POST['Webseite-P']);
			$member->Contact->WebsiteBusiness = htmlspecialchars($_POST['Webseite-G']);
		}
	}


	/**
	 * Fill the data from POST to MemberData
	 */
	function set_POSTtoMemberData($member) {
		if(strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') {
			// MemberState.
			If (isset($_POST['MemberState'])) {
				$arr = explode("-", $_POST['MemberState']);
				$member->MemberStateID = htmlspecialchars($arr[0]);
				$member->MemberState = htmlentities($arr[1]);
			}
				
			// FamilyFlag.
			If (isset($_POST['FamilyFlag'])) {
				$member->FamilyFlag = htmlspecialchars($_POST['FamilyFlag']);
			}
				
			// Associations:
			//   - AssociationList: AssociationTypeName=>AssociationNumber.
			//   - $_POST['AssociationTypes']: AssociationTypeID-AssociationTypeName.
			$member->Associations->AssociationList = Array();
			If (isset($_POST['AssociationTypes'])) {
				foreach ($_POST['AssociationTypes'] as $association) {
					// Add or update set associations.
					$member->Associations->AssociationList[$association] = htmlspecialchars($_POST[strtolower($association).'_license']);
				}
			}
				
			// Jobs
			// Delete all jobs and insert the new jobs.
			$member->Jobs->JobList = Array();
			$res = Array();
			If (isset($_POST['Jobs'])) {
				foreach ($_POST['Jobs'] as $jobitem) {
					// $jobitem: <JobID>-<JobName> (eg. "2-Webmaster")
					$job = explode('-', $jobitem);
					$res['UserID'] = htmlspecialchars($member->UserID);
					$res['JobID'] = htmlspecialchars($job[0]);
					$res['JobType'] = htmlspecialchars($job[1]);
					$member->Jobs->JobList[] = $res;
				}
			}
				
			// CommitteeTasks.
			// Delete all CommitteeTasks and insert the new tasks.
			$member->CommitteeTasks->CommitteeTaskList = Array();
			$res = Array();
			If (isset($_POST['CommitteeTasks'])) {
				foreach ($_POST['CommitteeTasks'] as $taskitem) {
					// $jobitem: <CommitteeTaskID>-<CommitteeTaskType> (eg. "1-Präsident")
					$task = explode('-', $taskitem);
					$res['MemberID'] = htmlspecialchars($member->MemberID);
					$res['CommitteeTaskID'] = htmlspecialchars($task[0]);
					$res['CommitteeTaskType'] = htmlspecialchars($task[1]);
					$member->CommitteeTasks->CommitteeTaskList[] = $res;
				}
			}
				
			// User-State.
			If (isset($_POST['State'])) {
				$member->State = htmlspecialchars($_POST['State']);
			}
		}
	}
	
	
?>